package com.qinghe.plant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinghe.plant.util.CodeType;
import com.qinghe.plant.util.Response;
import com.qinghe.plant.dao.Motion;
import com.qinghe.plant.model.MotionReponsitory;

@RestController
public class MotionController {

	@Autowired
	private MotionReponsitory motionReponsitory;
	
	@RequestMapping(value = "doReadMotions", method = RequestMethod.POST)
	public Response ReadMotions(HttpServletRequest request) {
		String mid =request.getParameter("mid");
		List<Motion> motions= FindMotions(mid);
		return new Response(CodeType.Success,JSONArray.toJSONString(motions),"操作成功");
	}
	
	@RequestMapping(value = "doReadMotion", method = RequestMethod.POST)
	public Response ReadMotion(HttpServletRequest request) {		
		String id =request.getParameter("id");
		String mid =request.getParameter("mid");
		Motion motion = FindMotion(id,mid);
		return new Response(CodeType.Success,JSONObject.toJSONString(motion),"操作成功");
	}

	@RequestMapping(value = "doDeleteMotion", method = RequestMethod.POST)
	public Response DeleteMotion(HttpServletRequest request) {
		String id =request.getParameter("id");
		motionReponsitory.deleteById(id);
		return new Response(CodeType.Success,null,"操作成功");
	}
	
	@RequestMapping(value="doAlterMotion",method=RequestMethod.POST)
	public Response AlterMotion(HttpServletRequest request)
	{
		Motion motion=JSONObject.parseObject(request.getParameter("motion"), Motion.class);
		motion= motionReponsitory.save(motion);
		return new Response(CodeType.Success,JSONArray.toJSONString(motion),"操作成功");		
	}
	
	@RequestMapping(value="doInsertMotion",method=RequestMethod.POST)
	public Response InsertMotion(HttpServletRequest request)
	{
		Motion motion=new Motion();
		motion.setMid(request.getParameter("mid"));
		motion.setName(request.getParameter("name"));
		motion.setPos(request.getParameter("pos"));
		motion.setCreator(request.getParameter("creator"));			
		motion.setTimer(new Date());
		motion= motionReponsitory.save(motion);
		return new Response(CodeType.Success,JSONObject.toJSONString(motion),"操作成功");
	}
	
	private List<Motion> FindMotions(String mid) {
		@SuppressWarnings("serial")
		List<Motion> result = motionReponsitory.findAll(new Specification<Motion>() {
			@Override
			public Predicate toPredicate(Root<Motion> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				list.add(cb.equal(root.get("mid").as(String.class), mid));

				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});
		return result;
	}		
	
	
	private Motion FindMotion(String id,String mid) {
		@SuppressWarnings("serial")
		List<Motion> result = motionReponsitory.findAll(new Specification<Motion>() {
			@Override
			public Predicate toPredicate(Root<Motion> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("id").as(String.class), id));
				list.add(cb.equal(root.get("mid").as(String.class), mid));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}

		});

		return (result.size()!=0)?result.get(0):null;
	}
	
}
